package com.example.dell1.startproject_jwq123.model;

import com.example.dell1.startproject_jwq123.bean.WeatherResult;
import com.example.dell1.startproject_jwq123.iface.WeatherIface;
import com.example.dell1.startproject_jwq123.iface.WeatherListener;
import com.example.dell1.startproject_jwq123.service.Weather51Service;
import com.example.dell1.startproject_jwq123.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dell1 on 2017/3/14.
 */

public class Weather51Model implements WeatherIface{
    private Retrofit retrofit;
    private String BASEURL="http://weather.51wnl.com/weatherinfo/";
    private Weather51Service weather51Service;

    public Weather51Model(){
        //Retrofit使用3-1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)//主机地址
                .addConverterFactory(GsonConverterFactory.create())//
                .build();
    }

    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
        //Retrofit使用3-2
        weather51Service = retrofit.create(Weather51Service.class);
        Call<WeatherResult> call=weather51Service.getResult(cityno,1);
        //Retrofit使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful()&& response.body()!=null)
                    listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }
}
